/**
 * Created by zhangligang
 * Email: tzhang2450@163.com
 * Date: 2018/8/7
 * Time: 上午5:44
 */
$(function () {

    var toolbar = '<div class="row">';
    toolbar += '<div class="col-sm-12">';
    toolbar += '<div class="col-sm-2">';
    toolbar += '</div>';
    toolbar += '<div class="col-sm-10">';
    toolbar += '<div class="row">';
    toolbar += '<div class="col-sm-12">';
    toolbar += '<div class="col-sm-6">';
    toolbar += '</div>';
    toolbar += '<div class="col-sm-6">';
    toolbar += '<select class="form-control select-status"><option value="">处理状态</option><option value="1">未回复</option><option value="2">处理中</option><option value="3">已完成</option></select>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';
    toolbar += '</div>';

    loadList('/v1/manager/repair/list.json', toolbar);

    $(document).on({
        change: function () {
            refreshList();
        }
    },'.select-status');

    $('#view-repair-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var that = $(this);
        sendAjax('/v1/manager/repair/detail.json', 'get', {id:id}, function(json){
            var data = json.body.data;
            var repair_type = getRepairType(data.type);
            that.find('.repair-id').val(data.id);
            that.find('.apart-name').text(data.name);
            that.find('.rent-user-name').text(data.rent_user_name+'（'+data.rent_user_number+'）');
            that.find('.rent-user-mobile').text(data.rent_user_mobile);
            that.find('.reply-at').text(data.reply_at);
            that.find('.repair-type').text(repair_type);
            that.find('.content').text(data.content);
            that.find('input:radio').each(function(){
                $(this).removeAttr('checked');
                if ($(this).val() == data.status) {
                    $(this).prop('checked', 'checked');
                }
            });
            that.find('textarea').val(data.remark);
        });
    });
    $('#view-repair-dialog').on('hide.bs.modal', function(){
        $(this).find('.apart-name').text('');
        $(this).find('.rent-user-name').text('');
        $(this).find('.rent-user-mobile').text('');
        $(this).find('.reply-at').text('');
        $(this).find('.repair-type').text('');
        $(this).find('.content').text('');
        $(this).find('input:radio').removeAttr('checked');
        $(this).find('input:radio:first').prop('checked', 'checked');
        $(this).find('textarea').val('');
    });
    $('#view-repair-dialog .btn-save').on('click', function(){
        var that = $('#view-repair-dialog');
        var data = {
            id: that.find('.repair-id').val(),
            status: that.find('input[name="status"]:checked').val(),
            remark: that.find('.remark').val()
        };
        if (checkForm(data)) {
            sendAjax('/v1/manager/change/repair/status', 'post', {data:data}, function(json){
                that.modal('hide');
                $('#action-tip').find('.tip-content').text(json.message);
                $('#action-tip').modal('show');
            });
        }
    });

    $('#delete-repair-dialog').on('show.bs.modal', function(event){
        var button = $(event.relatedTarget);
        var id = button.data('id');
        var name = button.data('name');
        $('.repair-id').val(id);
        $('.apart-name').text(name);
    });
    $('#delete-repair-dialog .btn-save').on('click', function(){
        var id = $('.repair-id').val();
        sendAjax('/v1/manager/delete/repair', 'post', {id:id}, function(json){
            $('#delete-repair-dialog').modal('hide');
            $('#action-tip').find('.tip-content').text(json.message);
            $('#action-tip').modal('show');
        });
    });
    $('#action-tip').on('hide.bs.modal', function(){
        $(this).find('.tip-content').text('');
        location.reload();
    });
});
function loadList(url, toolbar) {
    $("#repair-table").bootstrapTable({
        url: url,
        ajaxOptions:{
            headers: {"Authorization": $.cookie()._token}
        },
        pagination: true,
        toolbar: toolbar,
        iconSize: "outline",
        pageList: [10, 20, 50, 100],
        search: true,
        sortable: true,
        sortOrder: 'desc',
        striped: true,
        sortStable: true,
        sidePagination: 'server',
        queryParams : function (params) {
            params.status = $('.select-status').val();
            return params;
        },
        icons: {
            refresh: "glyphicon-repeat",
            columns: "glyphicon-list"
        },
        columns: [{
            field: 'id',
            title: 'ID'
        }, {
            field: 'name',
            title: '公寓名称'
        }, {
            field: 'type',
            title: '报修类型',
            formatter: function (value) {
                return getRepairType(value);
            }
        }, {
            field: 'status',
            title: '状态',
            formatter: function (value) {
                switch (value) {
                    case 1 : return '<span class="text-default"><b>未回复</b></span>';
                    case 2 : return '<span class="text-warning"><b>处理中</b></span>';
                    case 3 : return '<span class="text-success"><b>已完成</b></span>';
                    default : return '<span class="text-default"><b>未回复</b></span>';
                }
            }
        }, {
            field: 'reply_at',
            title: '回复时间'
        }, {
            field: 'id',
            title: '操作',
            formatter: function (value, row) {
                var action = '<a class="btn btn-xs btn-info view-repair" data-toggle="modal" data-target="#view-repair-dialog" data-id="'+row.id+'">详情</a>';
                if (row.status == 1) action += '<a class="btn btn-xs btn-danger delete-repair" data-toggle="modal" data-target="#delete-repair-dialog" data-id="'+row.id+'">删除</a>';
                return action;
            }
        }]
    });
}

function refreshList() {
    $('#repair-table').bootstrapTable('refresh');
}

function getRepairType(type) {
    switch (type) {
        case 0 : return '其他服务';
        case 1 : return '水电维修';
        case 2 : return '家电维修';
        case 3 : return '管道疏通';
        case 4 : return '空调维修';
        case 5 : return '开锁换锁';
        case 6 : return '内饰维修';
        case 7 : return '家具维修';
        case 8 : return '防水补漏';
        default : return '其他服务';
    }
}

function checkForm(data) {
    return true;
}